package com.conv.shop.business.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.crypto.GlobalBouncyCastleProvider;
import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    private final static Logger LOG = LoggerFactory.getLogger(JwtUtil.class);
    private static final String key = "shopxiaozu";
    public static String createToken(Integer consumerId,String phone){
        GlobalBouncyCastleProvider.setUseBouncyCastle(false);

        DateTime now = DateTime.now();
        DateTime expTime = now.offsetNew(DateField.HOUR,24);
        Map<String,Object> payload = new HashMap<>();
        //签发时间
        payload.put(JWTPayload.ISSUED_AT,now);
        //过期时间
        payload.put(JWTPayload.EXPIRES_AT,expTime);
        //生效时间
        payload.put(JWTPayload.NOT_BEFORE,now);
        //内容
        payload.put("consumerId",consumerId);
        payload.put("phone",phone);
        String token = JWTUtil.createToken(payload,key.getBytes());
        LOG.info("生成JWT token:{}",token);
        return token;
    }
    public static boolean validate(String token){
        GlobalBouncyCastleProvider.setUseBouncyCastle(false);
        try {
            JWT jwt = JWTUtil.parseToken(token).setKey(key.getBytes());
            //validate 包含了verify
            boolean validate = jwt.validate(0);
            LOG.info("JWT token校验结果：{}",validate);
            return validate;
        } catch (Exception e) {
            LOG.error("JWT token校验异常",e);
            return false;
        }
    }
    public static JSONObject getJSONObject(String token){
        GlobalBouncyCastleProvider.setUseBouncyCastle(false);

        JWT jwt = JWTUtil.parseToken(token).setKey(key.getBytes());
        JSONObject payloads = jwt.getPayloads();
        payloads.remove(JWTPayload.ISSUED_AT);
        payloads.remove(JWTPayload.EXPIRES_AT);
        payloads.remove(JWTPayload.NOT_BEFORE);
        LOG.info("根据token获取原始内容：{}",payloads);
        return payloads;
    }

    public static String getUserNameByToken(String token){
        JWT jwt = JWTUtil.parseToken(token).setKey(key.getBytes());
        return jwt.getPayload("consumerId").toString();
    }

}
